﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Commons.Web;
using Background.Core.DAL;
using Commons.Entity;
using Background.Core.Entity;
namespace Background.Core.Controllers
{
    public class WebDepartmentsController : BaseController
    {
        WebDepartmentsRepository webDepartmentsRepository;
        public WebDepartmentsController()
        {
            webDepartmentsRepository = new WebDepartmentsRepository();
        }

        public ActionResult Index(int? page = 1)
        {
            var model = webDepartmentsRepository.GetWebDepartments(
                null,
                new PagingParam(page.Value, base.PageSize));
            return View(model);
        }

        public ActionResult Details(int id)
        {
            return View(webDepartmentsRepository.GetEntity<WebDepartments>(id));
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            WebDepartments entity = new WebDepartments
            {
                Operator = base.Current_UserID,
                UpdateDate = DateTime.Now,
                Status = 100,
            };
            TryUpdateModel(entity);
            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add insert logic here
                    webDepartmentsRepository.Insert(entity);
                    return RedirectToAction("Index");
                }
                catch
                {
                    throw;
                }
            }
            return View();
        }



        public ActionResult Edit(int id)
        {
            return View();
        }



        [HttpPost]
        public ActionResult Edit(WebDepartments entity)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add update logic here
                    webDepartmentsRepository.Update<WebDepartments>(i => new WebDepartments
                    {
                        About = entity.About,
                        DepartmentID = entity.DepartmentID,
                        DepartmentName = entity.DepartmentName,
                        Operator = Current_UserName,
                        ParentID = entity.ParentID,
                        SortNumber = entity.SortNumber,
                        Status = entity.Status,
                        UpdateDate = entity.UpdateDate,
                    });
                }
                catch
                {
                    throw;
                }
            }
            return View();
        }



        public ActionResult Delete(int id)
        {
            try
            {
                webDepartmentsRepository.Delete(new WebDepartments { DepartmentID = id });
                return RedirectArtDialogToAction("操作成功", "Index");
            }
            catch (Exception)
            {

                return RedirectArtDialogToAction("操作失败", "Index");
            }
        }


    }
}
